package com.blackbird.jang;

import com.blackbird.jang.layer.GameLayer;
import com.blackbird.jang.layer.HelperLayer;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Menu;
import com.wiyun.engine.nodes.MenuItemSprite;
import com.wiyun.engine.nodes.Scene;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.transitions.ColorFadeTransition;
import com.wiyun.engine.transitions.TransitionScene;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.utils.ResolutionIndependent;

/**
 * 游戏主类，负责所有场景的切换
 * 
 * @created at 20111001
 * @author jang 
 * 
 */
public class MainActivity extends ActivityTemplate {

	public static float WIN_WIDTH;
	public static float WIN_HEIGHT;
	public static float SCALE_X;
	public static float SCALE_Y;


	@Override
	protected Layer createLayer() {
		WIN_WIDTH = Director.getInstance().getWindowSize().width;
		WIN_HEIGHT = Director.getInstance().getWindowSize().height;
		SCALE_X = WIN_WIDTH / ResolutionIndependent.resolveDp(Globals.MAP_WIDTH);
		SCALE_Y = WIN_HEIGHT / ResolutionIndependent.resolveDp(Globals.MAP_HEIGHT);
		
		return new MenuLayer();
	}

	protected TransitionScene getTransition(Scene scene) {
		return (TransitionScene) ColorFadeTransition.make(2, scene,
				new WYColor3B(255, 255, 0)).autoRelease();
	}

	protected TransitionScene getBackTransition(Scene scene) {
		return (TransitionScene) ColorFadeTransition.make(2, scene,
				new WYColor3B(0, 255, 255)).autoRelease();
	}

	class MenuLayer extends Layer {
		public MenuLayer() {
			Sprite background = Sprite.make(R.drawable.logobg);
			background.setScale(SCALE_X, SCALE_Y);
			background.setPosition(WIN_WIDTH / 2, WIN_HEIGHT / 2);
			addChild(background);

			MenuItemSprite item1 = MenuItemSprite.make(R.drawable.starting, 0,
					this, "onStart");
			item1.setScale(SCALE_X, SCALE_Y);
			item1.setPosition(240 * SCALE_X, 470 * SCALE_Y);

			MenuItemSprite item2 = MenuItemSprite.make(R.drawable.setting, 0,
					this, "onHelp");
			item2.setScale(SCALE_X, SCALE_Y);
			item2.setPosition(240 * SCALE_X, 385 * SCALE_Y);

			MenuItemSprite item3 = MenuItemSprite.make(R.drawable.scoring, 0,
					this, "onQuit");
			item3.setScale(SCALE_X, SCALE_Y);
			item3.setPosition(240 * SCALE_X, 300 * SCALE_Y);

			Menu menu = Menu.make(item1, item2, item3);
			menu.alignItemsVertically();

			addChild(menu);
		}

		public void onStart() {
			Scene scene = Scene.make();
			scene.addChild(GameLayer.getInstance(), 0);
			scene.autoRelease(true);
			Director.getInstance().pushScene(getTransition(scene));
		}

		public void onHelp() {
			Scene scene = Scene.make();
			scene.addChild(HelperLayer.getInstance(), 0);
			scene.autoRelease(true);
			Director.getInstance().pushScene(getTransition(scene));
		}

		public void onQuit() {
			Director.getInstance().popScene();
		}
	}
}
